package com.ruyuan.engine.datagen;
import com.alibaba.fastjson.JSON;
import com.ruyuan.engine.pojo.ClientLog;
import com.ruyuan.engine.utils.Constants;
import com.ruyuan.engine.utils.HBaseUtils;
import com.ruyuan.engine.utils.KafkaProducerUtils;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.kafka.clients.producer.Producer;
import org.apache.kafka.clients.producer.ProducerRecord;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 *  生成用户画像数据
 *      create_namespace 'events_db'
 *      create 'events_db:users','profile','region','registration'
 *      registration:imei -> 登录设备
 *
 *      删除：
 *          disable 'events_db:users'
 *          drop 'events_db:users'
 */
public class MockUserPortrait {
    static HTable hTable = HBaseUtils.initHbaseClient(Constants.HBASE_TABLE);

    public static void main(String[] args) throws InterruptedException {
//        mockData();
//        checkData();
    }

    private static void checkData() {
        String region = HBaseUtils.getCell(Constants.HBASE_TABLE, "ggBGAGBe","region","historyloc");
        String profile = HBaseUtils.getCell(Constants.HBASE_TABLE, "ggBGAGBe","profile","tagCondition50");
        System.out.println("region ="+region + " profile="+profile);
    }

    private static void mockData()  {
        List<Put> puts = new ArrayList<>();
        for(int i=0;i<100;i++) {
            String userId = MockDataUtils.userId();
            Put put = new Put(Bytes.toBytes(userId));
            String key = "historyloc";
            String locations = MockDataUtils.province() + "|" + MockDataUtils.province() + "|"+ MockDataUtils.province() + "|"+ MockDataUtils.province();
            put.addColumn(Bytes.toBytes("region"),Bytes.toBytes(key),Bytes.toBytes(locations));
            puts.add(put);
            generateProfile(put,puts);
        }
        if (puts.size() > 0) {
            try {
                hTable.put(puts);
            }catch (Exception e) {
                e.printStackTrace();
            }

        }
    }

    private static void generateProfile(Put put, List<Put> puts) {
        for (int i = 1; i <= 100 ; i++) {
            String key = "tagCondition"+i;
            String value = "tagValue"+i;
            put.addColumn(Bytes.toBytes("profile"),Bytes.toBytes(key),Bytes.toBytes(value));
        }
        puts.add(put);
    }


    /**
     * 发送重复数据
     * */
    private static ClientLog getClientLogOneMessage() {
        ClientLog clientLog = new ClientLog();
        String userID = MockDataUtils.userId();
        clientLog.setUserNo("2A7eGfG1");
        clientLog.setUserName("张三");
        clientLog.setAppId(MockDataUtils.getAppID());
        clientLog.setAddr(MockDataUtils.cities[MockDataUtils.getRandNum(9, 0)] + MockDataUtils.getRoad());
        clientLog.setAppVersion(MockDataUtils.getAppVersionID());
        clientLog.setCarrier(MockDataUtils.getCarrier());
        clientLog.setImei(userID);
        clientLog.setDeviceType(MockDataUtils.getDeviceType());
        clientLog.setIp(MockDataUtils.getRandomIp());
        clientLog.setNetType(MockDataUtils.network());
        clientLog.setOsName(MockDataUtils.getOsNameType());
        clientLog.setOsVersion(MockDataUtils.getOsVersion());
        clientLog.setSessionId(MockDataUtils.getRandomNumAndCharacters(16));
        // 时间
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyMMdd");
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyyMMddHHmmss");
        //创建详细时间
        clientLog.setDetailTime(String.valueOf(MockDataUtils.randomDateDetail("20190217000000", "20210217000000", sdf2)));
        //创建日期
        clientLog.setCreateTime(System.currentTimeMillis());
        clientLog.setGps(MockDataUtils.randomLonLat(3.51, 53.33, 73.33, 135.05, "Lon+lat"));
        clientLog.setEventId(RandomStringUtils.randomAlphabetic(1).toUpperCase());
        clientLog.setEventType("login");
        return clientLog;
    }

    /**
     * 生成新的数据
     * */
    private static ClientLog getClientLog() {
        ClientLog clientLog = new ClientLog();
        String userID = MockDataUtils.userId();
        clientLog.setUserNo(MockDataUtils.userId());
        clientLog.setUserName(MockDataUtils.getChineseName());
        clientLog.setAppId(MockDataUtils.getAppID());
        clientLog.setAddr(MockDataUtils.cities[MockDataUtils.getRandNum(9, 0)] + MockDataUtils.getRoad());
        clientLog.setAppVersion(MockDataUtils.getAppVersionID());
        clientLog.setCarrier(MockDataUtils.getCarrier());
        clientLog.setImei(userID);
        clientLog.setDeviceType(MockDataUtils.getDeviceType());
        clientLog.setIp(MockDataUtils.getRandomIp());
        clientLog.setNetType(MockDataUtils.network());
        clientLog.setOsName(MockDataUtils.getOsNameType());
        clientLog.setOsVersion(MockDataUtils.getOsVersion());
        clientLog.setSessionId(MockDataUtils.getRandomNumAndCharacters(16));
        // 时间
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyMMdd");
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyyMMddHHmmss");
        //创建详细时间
        clientLog.setDetailTime(String.valueOf(MockDataUtils.randomDateDetail("20190217000000", "20210217000000", sdf2)));
        //创建日期
        clientLog.setCreateTime(System.currentTimeMillis());
        clientLog.setGps(MockDataUtils.randomLonLat(3.51, 53.33, 73.33, 135.05, "Lon+lat"));
        clientLog.setEventId(RandomStringUtils.randomAlphabetic(1).toUpperCase());
        clientLog.setEventType("login");
        return clientLog;
    }
}
